#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include <memory>
#include "../mqcomm/Message.pb.h"
#include "../mqserver/message.hpp"
using namespace std;
rbmq::MessageManager::ptr mmg;

class MessageTest : public testing::Environment {
public :
    virtual void SetUp() override {
        mmg = make_shared<rbmq::MessageManager>("./data/message/");
        mmg->initQueueMessage("queue1");
        mmg->initQueueMessage("queue2");
    }
    virtual void TearDown() override {
        //mmg->clear();

    }
private :
};

TEST(message_test, insert_test) {
    mqNs::BasicProperties bp;
    bp.set_id(UuidHelper::uuid());
    bp.set_delivery_mode(mqNs::DeliveryMode::DURABLE);
    bp.set_routing_key("music.game#sport");
    mmg->insert("queue1", &bp, "hello world1!", mqNs::DeliveryMode::DURABLE);
    mmg->insert("queue1", nullptr, "hello world2!", mqNs::DeliveryMode::DURABLE);
    mmg->insert("queue1", nullptr, "hello world3!", mqNs::DeliveryMode::DURABLE);
    mmg->insert("queue1", nullptr, "hello world4!", mqNs::DeliveryMode::DURABLE);
    mmg->insert("queue1", nullptr, "hello world5!", mqNs::DeliveryMode::UNDURABLE);

    mmg->insert("queue2", nullptr, "hello world5!", mqNs::DeliveryMode::DURABLE);
    mmg->insert("queue2", nullptr, "hello world4!", mqNs::DeliveryMode::DURABLE);
    mmg->insert("queue2", nullptr, "hello world3!", mqNs::DeliveryMode::UNDURABLE);

    ASSERT_EQ((int)mmg->total_count("queue1"), 4);
    ASSERT_EQ((int)mmg->durable_count("queue1"), 4);
    ASSERT_EQ((int)mmg->waitack_count("queue1"), 0);
    ASSERT_EQ((int)mmg->push_count("queue1"), 5);
    rbmq::MessagePtr msg = mmg->front("queue1");
    string msg_id = msg->payload().properties().id();
    ASSERT_EQ((int)mmg->waitack_count("queue1"), 1);
    mmg->ack("queue1", msg_id);
    ASSERT_EQ((int)mmg->waitack_count("queue1"), 0);


    ASSERT_EQ((int)mmg->total_count("queue2"), 2);
    ASSERT_EQ((int)mmg->durable_count("queue2"), 2);
    ASSERT_EQ((int)mmg->push_count("queue2"), 3);
    ASSERT_EQ((int)mmg->waitack_count("queue2"), 0);
}

// TEST(message_test, recovery_test) {
//     ASSERT_EQ(mmg->total_count("queue1"), 4);
//     ASSERT_EQ(mmg->durable_count("queue1"), 4);
//     ASSERT_EQ(mmg->waitack_count("queue1"), 0);
//     rbmq::MessagePtr msg = mmg->front("queue1");
//     ASSERT_EQ(msg->payload().properties().delivery_mode(), mqNs::DeliveryMode::DURABLE);
//     ASSERT_EQ(msg->payload().body(), string("hello world1!"));
//     string msg_id = msg->payload().properties().id();
//     mmg->ack("queue1", msg_id);
//     ASSERT_EQ(mmg->durable_count("queue1"), 3);

//     // ASSERT_EQ((int)mmg->total_count("queue1"), 4);
//     // ASSERT_EQ((int)mmg->durable_count("queue1"), 4);
//     // ASSERT_EQ((int)mmg->waitack_count("queue1"), 0);
//     // ASSERT_EQ((int)mmg->push_count("queue1"), 4);


//     // ASSERT_EQ((int)mmg->total_count("queue2"), 2);
//     // ASSERT_EQ((int)mmg->durable_count("queue2"), 2);
//     // ASSERT_EQ((int)mmg->push_count("queue2"), 2);
//     // ASSERT_EQ((int)mmg->waitack_count("queue2"), 0);
// }

// TEST(message_test, delete_test) {
//     mmg->ack("queue1", "1da7a706-3f26-3d06-0000-000000000001");
//     ASSERT_EQ(mmg->durable_count("queue1"), 4);

// }



int main(int argc, char* args[]) {
    testing::InitGoogleTest(&argc, args);
    testing::AddGlobalTestEnvironment(new MessageTest());
    RUN_ALL_TESTS();
    return 0;
}
